package com.china08.yxyapi.api.manage.edu.platform.statistics.resourcepandect1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResType;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResTypeRepository;
import com.china08.yxyapi.service.yxyresource.statistics.ResourceTypeCountService;

/**
 * 
 * @Description 教育局平台办公 资源统计（饼状图）资源类型分布
 *
 * @Author LiuWeidong
 *
 * @Date 2017年12月8日
 *
 */
@CrossOrigin
@RestController
public class ResourceTypeCount1Controller extends YxyApiBaseController {

    @Autowired
    public ResourceTypeCountService resourceTypeCountService;

    @Autowired
    private YxyResTypeRepository yxyResTypeRepository;

    @RequestMapping(value = "/api_yw/manage/edu/platform/statistics/resourcepandect1/resourcetype/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel req) {

        RespModel model = new RespModel();
        Integer schoolBureauLevel = getProfile(request, response).getSchoolBureauLevel();
        String districtCode = null;
        if (schoolBureauLevel == 0) {
            districtCode = getProfile(request, response).getSchoolAreaIdDistrict();
        } else if (schoolBureauLevel == 1) {
            districtCode = getProfile(request, response).getSchoolAreaIdCity();
        } else if (schoolBureauLevel == 2) {
            districtCode = getProfile(request, response).getSchoolAreaIdProvince();
        }
        Map<String, Long> countLst = null;

        DecimalFormat df = new DecimalFormat("######0.00");

        // 饼形图
        List<Object[]> countLstObj = new ArrayList<Object[]>();
        // 名称和具体数据
        List<Object[]> countLstObj2 = new ArrayList<Object[]>();
        // 调用此方法查询资源数据总览数据
        if (req.stageName == null) {
            countLst = resourceTypeCountService.resourceTypeCount(req.stageName, schoolBureauLevel, districtCode);
            Integer zong = 0;
            for (String key1 : countLst.keySet()) {
                zong += Integer.parseInt(countLst.get(key1).toString());
            }
            Object[] objs = null;
            Object[] objs2 = null;
            for (String key : countLst.keySet()) {
                objs = new Object[2];
                objs2 = new Object[2];
                YxyResType yxyResFormat = yxyResTypeRepository.findById(key);
                if (yxyResFormat != null) {
                    objs[0] = yxyResFormat.getName();
                    objs2[0] = yxyResFormat.getName();
                    objs[1] = Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
                    objs2[1] = countLst.get(key).toString();
                    countLstObj.add(objs);
                    countLstObj2.add(objs2);
                }

            }
        } else {
            countLst = resourceTypeCountService.resourceTypeCount(req.stageName, schoolBureauLevel, districtCode);
            Integer zong = 0;
            for (String key1 : countLst.keySet()) {
                zong += Integer.parseInt(countLst.get(key1).toString());
            }
            Object[] objs = null;
            Object[] objs2 = null;
            for (String key : countLst.keySet()) {
                objs = new Object[2];
                objs2 = new Object[2];
                YxyResType yxyResFormat = yxyResTypeRepository.findById(key);

                if (yxyResFormat != null) {
                    objs[0] = yxyResFormat.getName();
                    objs2[0] = yxyResFormat.getName();
                    objs[1] = Double.parseDouble(df.format(Double.parseDouble(countLst.get(key).toString())));
                    objs2[1] = countLst.get(key).toString();
                    countLstObj.add(objs);
                    countLstObj2.add(objs2);
                }

            }
        }

        model.countLstObj = countLstObj;
        model.countLstObj2 = countLstObj2;
        return model;

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 学段名称
        public String stageName;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 资源类型饼形图的数据
        public List<Object[]> countLstObj;
        // 名称和具体数据
        public List<Object[]> countLstObj2;
    }
}
